package com.awwork.lr.checker.tools;

//import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * ���ڹ�����
 * 
 * @author MaChao
 * 
 */
public class DateTool {

	private static GregorianCalendar gc = null;

	static {
		gc = new GregorianCalendar(Locale.CHINA);
		gc.setLenient(true);
		gc.setFirstDayOfWeek(Calendar.MONDAY);
	}

	private static void initCalendar(Date date) {
		if (date == null) {
			throw new IllegalArgumentException("argument date must be not null");
		}
		gc.clear();
		gc.setTime(date);
	}

	/**
	 * ���ش������ڵĺ�һ��
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 * @author MaChao
	 */
	public static Date getNextDay(Date date) {
		initCalendar(date);
		long tomorrow = gc.getTimeInMillis() + 1 * 60 * 60 * 24 * 1000;
		gc.setTimeInMillis(tomorrow);
		return gc.getTime();
	}

	/**
	 * ���ش������ڵ�ǰһ��
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 * @author MaChao
	 */
	public static Date getLastDay(Date date) {
		initCalendar(date);
		long yestarday = gc.getTimeInMillis() - 1 * 60 * 60 * 24 * 1000;
		gc.setTimeInMillis(yestarday);
		return gc.getTime();
	}

	/**
	 * ������������µ����һ��
	 * 
	 * @param String
	 *            dateStr(yyyy-MM-dd)
	 * @return String(yyyy-MM-dd)
	 * @throws ParseException
	 * @author MaChao
	 */
	public static String getLastMonthDay(String dateStr) throws ParseException {
		Date date = praseString2Date(dateStr);
		Date lastMonthDay = getLastMonthDay(date);
		return formatDate2ShortString(lastMonthDay);
	}

	/**
	 * ��Date��ʽ��Ϊ����Integer
	 * 
	 * @param Date
	 *            date
	 * @return Integer(yyyyMMdd)
	 * @author MaChao
	 */
	public static Integer formatDate2Integer(Date date) {
		initCalendar(date);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Integer dateInt = Integer.parseInt(sdf.format(date));
		return dateInt;
	}

	
	/**
	 * ��Date��ʽ��Ϊ����String
	 * 
	 * @param Date
	 *            date
	 * @return String(yyyy-MM-dd)
	 * @author MaChao
	 */
	public static String formatDate2ShortString(Date date) {
		initCalendar(date);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String dateStr = sdf.format(date);
		return dateStr;
	}

	/**
	 * �������ڸ�ʽ��Stringת��ΪDate����
	 * 
	 * @param String
	 *            dateStr(yyyy-MM-dd)
	 * @return Date
	 * @throws ParseException
	 * @author MaChao
	 */
	public static Date praseString2Date(String dateStr) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = (Date) sdf.parse(dateStr);
		return date;
	}

	/**
	 * ��Date��ʽ��Ϊ����String
	 * 
	 * @param date
	 * @return
	 * @author baiys
	 */
	public static String formatDate2LongString(Date date) {
		initCalendar(date);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateStr = sdf.format(date);
		return dateStr;
	}

	/**
	 * �������ڸ�ʽ��Stringת��ΪDate����
	 * 
	 * @param String
	 *            dateStr(yyyy-MM-dd HH:mm:ss)
	 * @return Date
	 * @throws ParseException
	 * @author MaChao
	 */
	public static Date praseString2LongDate(String dateStr)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = (Date) sdf.parse(dateStr);
		return date;
	}

	/**
	 * ȡ�����������ܵĵ�һ��
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getFirstWeekDay(Date date) {
		initCalendar(date);
		gc.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return gc.getTime();
	}

	/**
	 * ȡ�����������ܵ����һ��
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getLastWeekDay(Date date) {
		initCalendar(date);
		gc.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return gc.getTime();
	}

	/**
	 * ȡ�����������µĵ�һ��
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getFirstMonthDay(Date date) {
		initCalendar(date);
		int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
		gc.add(Calendar.DAY_OF_MONTH, 1 - dayOfMonth);
		return gc.getTime();
	}

	/**
	 * ȡ�����������µ����һ��
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getLastMonthDay(Date date) {
		initCalendar(date);
		int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
		int maxDaysOfMonth = gc.getActualMaximum(Calendar.DAY_OF_MONTH);
		gc.add(Calendar.DAY_OF_MONTH, maxDaysOfMonth - dayOfMonth);
		return gc.getTime();
	}

	/**
	 * ȡ����������Ѯ�����һ��
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getFirstTenDaysDay(Date date) {
		initCalendar(date);
		int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
		if (dayOfMonth <= 10) {
			gc.set(Calendar.DAY_OF_MONTH, 1);
		} else if (dayOfMonth > 20) {
			gc.set(Calendar.DAY_OF_MONTH, 21);
		} else {
			gc.set(Calendar.DAY_OF_MONTH, 11);
		}
		return gc.getTime();
	}

	/**
	 * ȡ����������Ѯ�����һ��
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getLastTenDaysDay(Date date) {
		initCalendar(date);
		int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
		if (dayOfMonth <= 10) {
			gc.set(Calendar.DAY_OF_MONTH, 10);
		} else if (dayOfMonth > 20) {
			gc.set(Calendar.DAY_OF_MONTH, gc
					.getActualMaximum(Calendar.DAY_OF_MONTH));
		} else {
			gc.set(Calendar.DAY_OF_MONTH, 19);
		}
		return gc.getTime();
	}

	/**
	 * ��ʱ���ַ�õ�Date��ʱ�䣬ʱ���ʽyyyy-MM-dd
	 * 
	 * @param dateStr
	 * @return
	 * @throws Exception
	 */
	/*
	 * public static Date getDateFromStr(String dateStr) throws Exception { try { //
	 * ����ֻ��֤��Ϊ�գ���������ǰ̨������֤��ʱ���ʽyyyy-MM-dd if (dateStr != null &&
	 * !dateStr.trim().equals("")) {
	 * 
	 * DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT,
	 * Locale.CHINESE); return dateFormat.parse(dateStr); // return new //
	 * java.sql.Date(dateFormat.parse(dateStr).getTime()); } else { return null; } }
	 * catch (Exception ex) { throw new Exception("--��������ַ�õ�Date����ʱ����--", ex); } }
	 */

	/**
	 * ��Date��ʱ��õ�ʱ���ַ�ʱ���ʽyyyy-MM-dd,
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	/*
	 * public static String getStrFromDate(Date date) throws Exception { try { //
	 * ����ֻ��֤��Ϊ�գ���������ǰ̨������֤��ʱ���ʽyyyy-MM-dd if (date != null) {
	 * 
	 * DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT,
	 * Locale.CHINESE); return dateFormat.format(date); // return new //
	 * java.sql.Date(dateFormat.parse(dateStr).getTime()); } else { return null; } }
	 * catch (Exception ex) { throw new Exception("--�������Date����õ��ַ�ʱ����--", ex); } }
	 */

	public static void main(String[] args) {
		Date date;
		try {
			date = praseString2Date("2009-10-31");
			System.out.println("Today = " + formatDate2ShortString(date));
			System.out.println("Yesterday = "
					+ formatDate2ShortString(getLastDay(date)));
			System.out.println("Tomorrow = "
					+ formatDate2ShortString(getNextDay(date)));
			System.out.println("getFirstWeekDay = " + getFirstWeekDay(date));
			System.out.println("getLastWeekDay = " + getLastWeekDay(date));
			System.out.println("getFirstMonthDay = " + getFirstMonthDay(date));
			System.out.println("getLastMonthDay = " + getLastMonthDay(date));
			System.out.println("getFirstTenDaysDay = "
					+ getFirstTenDaysDay(date));
			System.out
					.println("getLastTenDaysDay = " + getLastTenDaysDay(date));

			System.out.println("getLastMonthDay = "
					+ getLastMonthDay("2008-2-2"));
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}
}
